<?php
	include_once("bin/connexion.php");
	
	if(isset($_SESSION["user_id"]))
	{
		$id = $_SESSION["user_id"];
		$query = "SELECT * FROM user where id='$id'";
		$result = mysqli_query($bdd,$query);
		$user = mysqli_fetch_array($result);
		$num_biens = 0;
		
		if($user["bailleur"] == 1)
		{
			$query = "SELECT * FROM bien where id_owner='$id'";
			$result = mysqli_query($bdd,$query);
			$num_biens = mysqli_num_rows($result);
		}
		
		$infos = array($user["nom"], $user["prenom"], $user["bailleur"], $user["pic"], $num_biens, $user["mail"], $user["password"]);
	}
	else
	{
		header("location: freebed-login.php");
	}
	
	//Insert BDD
	if(isset($_POST["envoyer"]))
	{
		$type = $_POST["type"];
		$surface = $_POST["surface"];
		$adresse = $_POST["adresse"];
		$ville = $_POST["ville"];
		$cp = $_POST["cp"];
		$pays = $_POST["pays"];
		$prix1 = $_POST["prix1"];
		$prix2 = $_POST["prix2"];
		$description = $_POST["description"];
		
		//insert bdd ville
		$query = "SELECT * FROM ville where libelle='$ville'";
		$resultat = mysqli_query($bdd, $query);
		if(mysqli_num_rows($resultat) == 0) //ville inexistante
		{
			//ajout d'une ville
			$query = "INSERT INTO ville (id, libelle, id_pays) VALUES ('', '$ville', '$pays')";
			$resultat = mysqli_query($bdd, $query);
		}
		
		//recup id ville
		$query = "SELECT id FROM ville where libelle='$ville'";
		$resultat = mysqli_query($bdd, $query);
		$data = mysqli_fetch_array($resultat);
		$id_ville = $data[0];
		
		//insert bdd bien
		$query = "INSERT INTO bien (id, id_owner, is_stand_by, id_type, surface, adresse, CP, prix_day, prix_week, id_ville, id_photos) VALUES ('', '$id', 1, '$type', '$surface', '$adresse', '$cp', '$prix1', '$prix2', '$id_ville', 0)";
		$resultat = mysqli_query($bdd, $query);
		
		if ($resultat == true)
		{
			//recup l'id_bien
			$id_bien = mysqli_insert_id($bdd);
			
			if($user["bailleur"] == 0)
			{
				//on set l'user en mode bailleur
				$query = "UPDATE user SET bailleur=1 WHERE id='$id'";
				mysqli_query($bdd, $query);
			}
			
			//UPLOAD IMAGE
			if(isset($_FILES["img"]))
			{
				$arrayImg = array("gif", "jpg", "jpeg", "png");
				if($_FILES["img"]["size"] > 0)
				{
					$uploadfile = "img/house_pic/" . $id . "_" . $id_bien . "_cover.jpg";
					$file_extension = pathinfo($uploadfile, PATHINFO_EXTENSION);
					
					if(in_array($file_extension, $arrayImg) && move_uploaded_file($_FILES["img"]["tmp_name"], $uploadfile))
					{
						//OK
						mysqli_query($bdd, "INSERT INTO photos (id, id_bien, path) VALUES ('', '$id_bien', '$uploadfile')");
						//recup l'id_photos & update photos bdd
						$id_photos = mysqli_insert_id($bdd);
						mysqli_query($bdd, "UPDATE bien SET id_photos='$id_photos' WHERE id='$id_bien'");
					}
				}
				else
				{
					//Aucune image on set une image par default
					mysqli_query($bdd, "INSERT INTO photos (id, id_bien) VALUES ('', '$id_bien')");
					//recup l'id_photos & update photos bdd
					$id_photos = mysqli_insert_id($bdd);
					mysqli_query($bdd, "UPDATE bien SET id_photos='$id_photos' WHERE id='$id_bien'");
				}
			}
		}
	}
	
	//Recup type_bien
	$query = "SELECT * FROM type_bien";
	$types = mysqli_query($bdd,$query);
	
	//Recup pays
	$query = "SELECT * FROM pays";
	$pays = mysqli_query($bdd,$query);
	
	//update la session
	if(isset($_SESSION["user_id"]))
	{
		$id = $_SESSION["user_id"];
		$query = "SELECT * FROM user where id='$id'";
		$result = mysqli_query($bdd,$query);
		$user = mysqli_fetch_array($result);
		$num_biens = 0;
		
		if($user["bailleur"] == 1)
		{
			$query = "SELECT * FROM bien where id_owner='$id'";
			$result = mysqli_query($bdd,$query);
			$num_biens = mysqli_num_rows($result);
		}
		
		$infos = array($user["nom"], $user["prenom"], $user["bailleur"], $user["pic"], $num_biens, $user["mail"], $user["password"]);
	}
	
	include_once("freebed-add.html");
?>